package core;

import java.util.HashMap;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;



public class Player extends Unit{
	public Image tex;
	public float right;
	public int width;
	public int height;
	public float feet;
	public float head;
	public Point center;
	public Point position;
	public HashMap<String,Item> items;
	public float fall;
	public int hp;
	public int maxHp;
	public String name;
	public boolean falling = false;
	public long falltime = 0;
	public float fallspeed = 0;
	public float scale;
	long lastScale;
	int d = Input.KEY_D;
	int a = Input.KEY_A;
	int s = Input.KEY_S;
	int w = Input.KEY_W;
	float x;
	float y;
	float acc = .00000000000002f;
	Gayme gc;
	public boolean jumping = false;
	public long jTime = 0;
	public float jSpeed = 20;
	public long jStart  = -1;
	public long jRelease = -1;
	boolean canJump = true;

	public Player(float x, float y, String tex, Gayme gc) throws SlickException{
		maxHp = 100;
		hp = 100;
		this.tex = new Image(tex);
		this.x = x;
		this.y = y;
		this.gc = gc;
		height = this.tex.getHeight();
		width = this.tex.getWidth();
		feet = y+height;
		right = x+width;
	}

	public void move(Input in){
		
		float below = getBelow();
		float r = getRight();
		float left = getLeft();
		System.out.println("fallspeed"+ fallspeed);
		
		if(in.isKeyDown(Input.KEY_SPACE) && !falling){
			float above = getAbove();
			falltime = 0;
			fallspeed=0;
			jTime +=System.currentTimeMillis();
			jumping = true;
			jSpeed-=jTime*acc;
			y-=jSpeed;
			//bottom of window collision detection
			if(y>=below-height){
				y = below-height;
				falltime = System.currentTimeMillis();
				falling = true;
				jumping = false;
			}
			//check if block is above
			else if(y<=above && above!=-1){
				y = above;
				falltime = System.currentTimeMillis();
				falling = true;
				jumping = false;
			}
			
			if(in.isKeyDown(d))
				if (right+5>=r)
					x = r-1;
				else
					x+=3f;
			if(in.isKeyDown(a))
				if(x-3<=left)
					x = left+1;
				else
					x-=3f;
		}
		else{
			jumping = false;
			jTime = 0;
			jSpeed = 10f;
			falling = true;
			falltime += System.currentTimeMillis();
			fallspeed+=falltime*acc;
			if(y+height<below){
				y+=fallspeed;
			}
			else if(y+height>below){
				y = below-height;
				falling = false;
				falltime = 0;
				fallspeed = 1f;
			}
			else{
				falling = false;
				falltime = 0;
				fallspeed = 1f;
				canJump=true;
			}
			if(in.isKeyDown(a))
				if(x-3<=left)
					x = left+1;
				else
					x-=3f;
			if(in.isKeyDown(d))
				if (right+5>=r)
					x = r-width-1;
				else
					x+=3f;
		}
		feet = y+height;
		right = x+width;
	}
	
	/**
	 * 
	 * @return y value of max height the player can go
	 */
	public float getAbove(){
		float closest = -1;
		for (Block b : gc.blocks){
			if(y>b.bottom && b.bottom > closest && right>b.x && x<b.right)
				closest = b.bottom;
		}
		if(closest == -1)
			return 0f;
		return closest;
	}
	
	public float getBelow(){
		float closest = -1f;
		for (Block b : gc.blocks){
			if(y<=b.y && (closest>b.y || closest == -1) && right>b.x && x<b.right)
				closest = b.y;
		}
		if(closest == -1){
			return gc.getHeight();
		}
		return closest;
	}
	
	public float getRight(){
		float closest = -1f;
		for (Block b : gc.blocks){
			if(right < b.x && (closest>=b.x || closest == -1) && y+height>b.y && y<b.bottom)
				closest = b.x;
		}
		if(closest == -1){
			return gc.getWidth();
		}
		return closest;
	}
	
	public float getLeft(){
		float closest = -1f;
		for (Block b : gc.blocks){
			if(x > b.right && (closest<=b.right || closest == -1) && y+height>b.y && y<b.bottom)
				closest = b.right;
		}
		if(closest == -1){
			return 0;
		}
		return closest;
	}
}


